<template>
  <div>
    <Dropdown placement="bottomLeft" :overlayClassName="`${prefixCls}-dropdown-overlay`">
      <span :class="[prefixCls, `${prefixCls}--${theme}`]" class="flex">
        <img :class="`${prefixCls}__header`" :src="getUserInfo.avatar" />
        <span :class="`${prefixCls}__info hidden md:block`">
          <span :class="`${prefixCls}__name  `" class="truncate">
            {{ getUserInfo.realName }}
          </span>
        </span>
      </span>

      <!-- 后台的顶部头像点击展开菜单部分 -->
      <template #overlay>
        <Menu @click="handleMenuClick">
          <!-- 文档 -->
          <!-- <MenuItem
          key="doc"
          :text="t('layout.header.dropdownItemDoc')"
          icon="ion:document-text-outline"
          v-if="getShowDoc"
        /> -->
          <!-- <MenuDivider v-if="getShowDoc" /> -->
          <!-- <MenuItem
          v-if="getUseLockPage"
          key="lock"
          :text="t('layout.header.tooltipLock')"
          icon="ion:lock-closed-outline"
        /> -->
          <MenuItem
            key="personinfo"
            :text="t('layout.header.dropdownItemProfile')"
            icon="ion:person-outline"
          />
          <MenuItem
            key="changeProject"
            :text="t('layout.header.dropdownItemChangeProject')"
            icon="ion:arrow-swap"
          />
          <MenuItem
            key="modify"
            :text="t('layout.header.dropdownItemModifyPassword')"
            icon="ion:settings-outline"
          />
          <MenuItem
            key="logout"
            :text="t('layout.header.dropdownItemLoginOut')"
            icon="ion:power-outline"
          />
        </Menu>
      </template>
    </Dropdown>
    <LockAction @register="registerLock" />
    <ModifyPassword @register="registerModifyPassword" />
    <ChangeProjectModal @register="registerChangeProjectModal" />
    <ChangePersonInfoModal @register="regitsterChangePersonInfo" />
  </div>
</template>
<script lang="ts">
  // components
  import { Dropdown, Menu } from 'ant-design-vue';

  import { defineComponent, computed, ref } from 'vue';

  import { DOC_URL } from '/@/settings/siteSetting';

  import { useUserStore } from '/@/store/modules/user';
  import { useHeaderSetting } from '/@/hooks/setting/useHeaderSetting';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useDesign } from '/@/hooks/web/useDesign';
  import { useModal } from '/@/components/Modal';

  import headerImg from '/images/header.jpg';
  import { propTypes } from '/@/utils/propTypes';
  import { openWindow } from '/@/utils';

  import { createAsyncComponent } from '/@/utils/factory/createAsyncComponent';
  import { useRouter } from 'vue-router';
  import { useGlobSetting } from '/@/hooks/setting';
  import { useAppStoreWithOut } from '/@/store/modules/app';
  import { doLogout } from '/@/api/platform/uac';
  import ChangePersonInfoModal from '../changePersonInfo/ChangePersonInfoModal.vue';
  type MenuEvent = 'modify' | 'logout' | 'doc' | 'lock';
  import { getFileApi } from '/@/api/platform/file';

  export default defineComponent({
    name: 'UserDropdown',
    components: {
      Dropdown,
      Menu,
      MenuItem: createAsyncComponent(() => import('./DropMenuItem.vue')),
      MenuDivider: Menu.Divider,
      LockAction: createAsyncComponent(() => import('../lock/LockModal.vue')),
      ModifyPassword: createAsyncComponent(() => import('../modify/ModifyPassword.vue')),
      ChangeProjectModal: createAsyncComponent(
        () => import('../changeProject/ChangeProjectModal.vue'),
      ),
      ChangePersonInfoModal,
    },
    props: {
      theme: propTypes.oneOf(['dark', 'light']),
    },
    setup() {
      const { prefixCls } = useDesign('header-user-dropdown');
      const { t } = useI18n();
      const { getShowDoc, getUseLockPage } = useHeaderSetting();
      const userStore = useUserStore();
      const router = useRouter();
      const { globalProjectCode } = useGlobSetting();
      const { uploadUrl } = useGlobSetting();
      const getImageFileUrlPrefix = ref<string>(uploadUrl + getFileApi);

      const getUserInfo = computed(() => {
        const { realName = '', avatar, desc } = userStore.getUserInfo || {};
        return {
          realName,
          avatar: avatar ? getImageFileUrlPrefix.value + avatar : headerImg,
          desc,
        };
      });

      const [registerLock, { openModal: openLockModal }] = useModal();
      const [registerModifyPassword, { openModal: openModifyPasswordModal }] = useModal();
      const [registerChangeProjectModal, { openModal: openChangeProjectModal }] = useModal();
      const [regitsterChangePersonInfo, { openModal: openChangePersonInfoModal }] = useModal();

      function handleLock() {
        openLockModal(true);
      }

      function handleModifyPassword() {
        openModifyPasswordModal(true);
      }

      async function handleChangeProject() {
        openChangeProjectModal(true, {});
      }

      function handlePersonInfo() {
        openChangePersonInfoModal(true, {});
      }

      //  login out
      function handleLoginOut() {
        userStore.confirmLoginOut();
      }

      // open doc
      function openDoc() {
        openWindow(DOC_URL);
      }

      function handleMenuClick(e: { key: MenuEvent }) {
        switch (e.key) {
          case 'modify':
            handleModifyPassword();
            break;
          case 'logout':
            handleLoginOut();
            break;
          case 'changeProject':
            handleChangeProject();
            break;
          case 'doc':
            openDoc();
            break;
          case 'lock':
            handleLock();
            break;
          case 'personinfo':
            handlePersonInfo();
        }
      }

      return {
        prefixCls,
        t,
        getUserInfo,
        handleMenuClick,
        getShowDoc,
        registerLock,
        registerModifyPassword,
        registerChangeProjectModal,
        regitsterChangePersonInfo,
        getUseLockPage,
        globalProjectCode,
        getImageFileUrlPrefix,
      };
    },
  });
</script>
<style lang="less">
  @prefix-cls: ~'@{namespace}-header-user-dropdown';

  .@{prefix-cls} {
    height: @header-height;
    padding: 0 0 0 10px;
    padding-right: 10px;
    overflow: hidden;
    font-size: 12px;
    cursor: pointer;
    align-items: center;

    img {
      width: 24px;
      height: 24px;
      margin-right: 12px;
    }

    &__header {
      border-radius: 50%;
    }

    &__name {
      font-size: 14px;
    }

    &--dark {
      &:hover {
        background-color: @header-dark-bg-hover-color;
      }
    }

    &--light {
      &:hover {
        background-color: @header-light-bg-hover-color;
      }

      .@{prefix-cls}__name {
        color: @text-color-base;
      }

      .@{prefix-cls}__desc {
        color: @header-light-desc-color;
      }
    }

    &-dropdown-overlay {
      .ant-dropdown-menu-item {
        min-width: 160px;
      }
    }
  }
</style>
